package com.yiyi.credit.manage.sso.manager.controller;

import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.page.Pagination;
import com.yiyi.credit.manage.sso.model.dto.response.Response;
import com.yiyi.credit.manage.sso.model.entity.SysRole;
import com.yiyi.credit.manage.sso.model.entity.SysUserRole;
import com.yiyi.credit.manage.sso.service.inner.SysRoleService;
import com.yiyi.credit.manage.sso.service.inner.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class SysRoleController {

    //private static Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserRoleService sysUserRoleService;



    @RequestMapping(value = "/getSysRolePageList")
    public Object getSysRolePageList(@RequestParam Long sysInfoId, @RequestParam Integer pageIndex,
                                     @RequestParam Integer pageSize) throws Exception {

        Pagination pagination = new Pagination(pageIndex, pageSize);
        Pagination result = sysRoleService.getSysRolePageBySysId(sysInfoId, pagination);
        return new Response<>(result);
    }



    @RequestMapping(value = "/getSysRoleById")
    public Object getSysRoleById(@RequestParam String sysRoleId) throws Exception {

        SysRole sysRole = sysRoleService.getSysRoleById(Long.parseLong(sysRoleId));
        return new Response<>(sysRole);
    }



    @RequestMapping(value = "/addSysRole")
    public Object addSysRole(@RequestBody SysRole sysRole) throws Exception {

        if (sysRole == null) {
            return new Response<>(ResponseCode.NO_SYS_ROLE);
        }

        //增加角色表
        sysRoleService.addSysRole(sysRole);
        return new Response<>();
    }


    @RequestMapping(value = "/updateSysRole")
    public Object updateSysRole(@RequestBody SysRole sysRole) throws Exception {

        if (sysRole == null) {
            return new Response<>(ResponseCode.NO_SYS_ROLE);
        }

        sysRoleService.updateSysRole(sysRole);
        return new Response<>();
    }


    @RequestMapping(value = "/deleteSysRole")
    public Object deleteSysRole(@RequestBody SysRole sysRole) throws Exception {

        List<SysUserRole> sysUserRoleList = sysUserRoleService.getSysUserRoleByRoleId(sysRole.getId());
        if (sysUserRoleList != null && sysUserRoleList.size() > Constant.INT_ZERO) {
            return new Response<>(ResponseCode.SYS_ROLE_USERD);
        }
        sysRoleService.deleteSysRoleById(sysRole);
        return new Response<>();
    }




    //银交所系统调用


    @RequestMapping(value = "/getSysRoleList")
    public Object getSysRoleList(@RequestParam Long sysInfoId) throws Exception {

        List<SysRole> sysRoleList = sysRoleService.getSysRoleBySysId(sysInfoId);
        return new Response<>(sysRoleList);
    }




    @RequestMapping(value = "/getSubStationByUserId")
    public Object getSubStationByUserId(@RequestParam Long sysInfoId,@RequestParam Long userId) throws Exception {

        SysUserRole sysUserRole = sysUserRoleService.getSysUserRole(userId,sysInfoId).get(0);
        if(sysUserRole==null){
            return new Response<>(ResponseCode.USER_UN_ALLOT);
        }
        return new Response<>(sysUserRole);
    }

    //
    // @RequestMapping(value = "/addSysRole")
    // public Object addSysRole(@RequestParam Long sysInfoId,@RequestParam String roleName) throws Exception {
    //
    //     sysRoleService.addSysRole(sysInfoId,roleName);
    //     return new Response<>();
    // }


}
